//
//  main.m
//  Block类型
//
//  Created by apple on 15/8/10.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "RedWolf.h"

//int other = 123;

// block对象作为函数参数时；
//typedef int (^BlockType)(int a,int b);
//
//void math(int a,int b,BlockType op){
//    NSLog(@"result :%d",op(a,b));
//}
//
int main(int argc, const char * argv[]) {
    @autoreleasepool{
#if 0
    NSArray *array = @[@1, @2, @5, @3, @4];
    NSArray *sorted = [array sortedArrayUsingComparator:^NSComparisonResult(NSNumber *obj1, NSNumber *obj2) {
        return [obj1 compare:obj2];
    }];
    NSLog(@"%@",array);
    NSLog(@"--------------");
    NSLog(@"%@",sorted);
    
#endif
    
    {
#if 0      
        int (^add)(int,int) = ^int(int a ,int b)
        {
            return a + b;
        };
    
        int result = add(1,2);
        NSLog(@"result : %d",result);
#endif

#if 0
        NSLog(@"block外部：%p,%d",&other,other);//0x100001108,  123;
        void (^pBlock)() = ^{
            NSLog(@"block内部: %p,%d",&other,other);
            other = 234;
        };
        NSLog(@"block调用之前：%p,%d",&other,other);//0x100001108,   123;
        pBlock();
        NSLog(@"block调用之后：%p,%d",&other,other);
        
        other = 345;
        pBlock();
        
        //全局变量时，其值可在block内外部修改；变量的地址不会发生变化。
#endif
#if 0    //  静态变量时，变量的值可以在block对象的内外部进行修改，且变量地址不会发生变化；
        static int other = 123;
        NSLog(@"block外部：%p,%d",&other,other);
        void (^pBlock)() = ^{
            NSLog(@"block内部: %p,%d",&other,other);
            other = 234;
        };
        NSLog(@"block调用之前：%p,%d",&other,other);
        pBlock();
        NSLog(@"block调用之后：%p,%d",&other,other);
        
        other = 345;
        pBlock();
#endif
#if 0
        __block int other = 123;
        NSLog(@"block外部: %p: %d", &other, other);// 07fff5ffbff808 :123
        void (^pBlock)() = ^{
            NSLog(@"block内部: %p: %d", &other, other);
            other = 234;
        };
        NSLog(@"block调用之前: %p: %d", &other, other); //  0x10010da88: 123;  //在block创建后，只是把原变量的值从新赋值给了一个新的变量，只不过是他们名字相同而已；而后我们修改的都是对这个新的变量进行修改。
        pBlock();
        NSLog(@"block调用之后: %p: %d", &other, other);
        
        other = 345;
        pBlock();
#endif
#if 0
        math(11,2,^int(int a,int b){   //block 作为函数参数；
            return a + b;
        });
#endif
#if 0
        //1、普通的局部变量，不能在block中进行修改
        //2、普通的局部变量，在创建block对象时会在block 对象中复制产生一个只读的变量；
        int other = 123;
        NSLog(@"block外部 ： %p",&other);
        void(^pBlock)() = ^{
            NSLog(@"%d",other);
            NSLog(@"block内部: %p",&other);
        };
        pBlock();
        other = 345;
        pBlock();
#endif

#if 1
        RedWolf *redWolf = [RedWolf new];
        redWolf.catchBlock = ^{
            NSLog(@"灰太狼抓啊！！！");
        };
        [redWolf eatSheep];
#endif
    }

    return 0;
}
}